<?php

namespace App\Http\Middleware;

use App\System\Cis\Model\CisAdmin;
use App\System\Cis\Repositories\CisAdminRepository;
use Closure;
use Session;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Session::put('p_role', '{"\u7528\u6237\u7ba1\u7406":["\u8774\u8776\u4f1a\u5458","1\u7ea7\u5408\u4f19\u4eba","2\u7ea7\u5408\u4f19\u4eba"],"\u95e8\u5e97\u7ba1\u7406":["\u95e8\u5e97\u5217\u8868","\u95e8\u5e97\u5ba1\u6279"]}');
        if (empty(Session::get('adminid'))) {
            return redirect()->to('admin/platform/login');
        }
        $adminid = Session::get('adminid');
        $admininfo = CisAdmin::where('id',$adminid)->first();
        if(!$admininfo || $admininfo->status==1){
            return redirect()->route('admin.platform.login');
        }
        $allAuth = [
            'admin.index.index'       => ['admin.index.index'],
            'admin.member.index'      => ['admin.member.index', 'admin.member.export', 'admin.member.view', 'admin.member.changUserStatus', 'admin.member.cash', 'admin.member.editCash','admin.member.recommend','admin.member.transferCoin'],
            'admin.member.memberList' => ['admin.member.memberList', 'admin.member.addMember', 'admin.member.viewMember'],
            'admin.member.setWages'   => ['admin.member.setWages', 'admin.member.ajaxSetWages'],
            // 'admin.member.recommend'  => ['admin.member.recommend', 'admin.member.ajaxRecommend'],
            'admin.member.contact'    => ['admin.member.contact', 'admin.member.ajaxGetContact'],
            'admin.article.index'       => ['admin.article.index', 'admin.article.ajaxSave'],
            'admin.goods.index'      => ['admin.goods.index', 'admin.goods.add', 'admin.goods.edit','admin.goods.ajaxChangeSt','admin.goods.uploadFiles','admin.goods.ajaxSave'],
            'admin.order.index'        => ['admin.order.index', 'admin.order.view', 'admin.order.downLoad', 'admin.order.ajaxSave'],
            'admin.finance.index'    => ['admin.finance.index', 'admin.finance.downList'],
            'admin.finance.getCash'     => ['admin.finance.getCash', 'admin.finance.ajaxCheckGetCash'],
            'admin.finance.rechargeLog'       => ['admin.finance.rechargeLog', 'admin.finance.downRechargeLogList'],
            'admin.finance.integral'    => ['admin.finance.integral', 'admin.finance.ajaxInteCoin','admin.finance.downRegisterCoinList'],
            'admin.finance.registerCoin' => ['admin.finance.registerCoin', 'admin.finance.ajaxInteCoin','admin.finance.downRegisterCoinList'],
            'admin.finance.voucher' => ['admin.finance.voucher'],
            'admin.platform.position' => ['admin.platform.position','admin.platform.addPosition','admin.platform.editPosition','admin.platform.ajaxEditPosition','admin.platform.ajaxDelPosition'],
            'admin.platform.admin'    => ['admin.platform.admin', 'admin.platform.addAdmin', 'admin.platform.editAdmin', 'admin.platform.ajaxEditAdmin', 'admin.platform.ajaxChangeStatus', 'admin.platform.ajaxDelAdmin', 'admin.platform.ajaxGetModules'],
            'admin.platform.reSaleLog' => ['admin.platform.reSaleLog'],
            'admin.platform.adminLog'  => ['admin.platform.adminLog'],
            'admin.platform.messageLog'  => ['admin.platform.messageLog'],
            'admin.platform.holiday'  => ['admin.platform.holiday','admin.platform.delHoliday','admin.platform.addHoliday']
        ];
        $CisAdmin = CisAdminRepository::getInstance();
        $auth = $CisAdmin->getAuth($adminid);
        $module = \Route::currentRouteName();
        $isTurn = false;
        if ($auth != 'all' && $module != 'admin.platform.changePassword' && $module != 'admin.platform.ajaxChangePwd') {
            $available = [];
            foreach ($auth as $v) {
                foreach ($allAuth[$v] as $v2) {
                    array_push($available, $v2);
                }
            }
            if (!in_array($module, $available)) {
                $isTurn = true;
            }
        }
        if ($isTurn) {
            //echo '<script>alert("无权限！")</script>';
            return redirect()->route('admin.index.noAuth');
        }
        return $next($request);
    }
}
